package com.news.dao.impl;

import com.news.dao.CmsCategoryDao;
import com.news.dao.CmsCommentDao;
import com.news.entity.Category;
import com.news.entity.CategoryParam;
import com.news.entity.Comment;
import com.news.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:weiming
 * @Date:2021/3/29
 */
public class CmsCategoryDaoImpl implements CmsCategoryDao {
    private static final Connection CONNECTION = JdbcUtils.getConnection();

    @Override
    public List<Category> getAllCategory() {
        String sql = "SELECT * FROM cms_category";
        List<Category> list= new ArrayList<Category>();
        try {
            PreparedStatement pst = CONNECTION.prepareStatement(sql);
            ResultSet resultSet = pst.executeQuery();
            while (resultSet.next()){
                Category category = new Category();
                category.setId(resultSet.getInt(1));
                category.setName(resultSet.getString(2));
                category.setParentId(resultSet.getInt(3));
                category.setIsDeleted(resultSet.getInt(5));
                list.add(category);
            }
            resultSet.close();
            pst.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Category> getAllCategoryCanUse() {

        String sql = "SELECT * FROM cms_category where is_deleted = 0";
        List<Category> list= new ArrayList<Category>();
        try {
            PreparedStatement pst = CONNECTION.prepareStatement(sql);
            ResultSet resultSet = pst.executeQuery();
            while (resultSet.next()){
                Category category = new Category();
                category.setId(resultSet.getInt(1));
                category.setName(resultSet.getString(2));
                category.setParentId(resultSet.getInt(3));
                list.add(category);
            }
            resultSet.close();
            pst.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public int add(Category category) {
        String sql = "INSERT INTO cms_category (id, name, parentId, is_Delete) VALUES (?, ?, ?, ?) ";
        return 0;
    }

    @Override
    public int delete(int categoryId) {
        String sql = "UPDATE cms_category set is_Deleted = 1 WHERE id = ?";
        return 0;
    }

    @Override
    public int update(Category category) {
        String sql = "UPDATE cms_category set name = ?, parentId = ?, is_Deleted = 1 WHERE id = ?";
        return 0;
    }

    @Override
    public List<CategoryParam> getCategoryWithChild() {
        String sql = "SELECT * FROM cms_category  WHERE parentId = 0 and is_deleted = 0";
        List<CategoryParam> resultList = new ArrayList<>();
        try {
            PreparedStatement pst = CONNECTION.prepareStatement(sql);
            ResultSet resultSet = pst.executeQuery();

            while(resultSet.next()){
                CategoryParam categoryParent = new CategoryParam();
                categoryParent.setId(resultSet.getInt(1));
                categoryParent.setName(resultSet.getString(2));

                // 查询子ID
                String sql2 = "SELECT * FROM cms_category  WHERE is_deleted = 0 and parentId = " + resultSet.getInt(1);
                ResultSet rs = CONNECTION.prepareStatement(sql).executeQuery(sql2);
                List<Category> childList = new ArrayList<>();
                while(rs.next()){
                    Category categoryChild = new Category();
                    categoryChild.setId(rs.getInt(1));
                    categoryChild.setName(rs.getString(2));
                    childList.add(categoryChild);
                }
                categoryParent.setChildCategory(childList);
                resultList.add(categoryParent);
                rs.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        
        return resultList;
    }



}
